//
//  QYGDollRecordView.m
//  wawaji_ios
//
//  Created by 奇艺果 on 2017/12/28.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGDollRecordView.h"
#import "QYGDetailCatchCell.h"
#import "QYGGameRecordModel.h"
#import "QYGNetRequestModel.h"

static NSString *drollRecordID = @"drollRecordID";
@interface QYGDollRecordView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation QYGDollRecordView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self viewWithcornerRadius:8 borderWidth:0 borderColor:nil backgroundColor:[UIColor whiteColor]];
        [self setupUI];
        [self setupData];
    }
    return self;
}

- (void)dealloc{
    NSLog(@"QYGDollRecordView--销毁");
    [NetRequestClass cancelOperations];
}
#pragma mark- tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return ScaleW(70);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QYGDetailCatchCell *cell = [tableView dequeueReusableCellWithIdentifier:drollRecordID forIndexPath:indexPath];
    QYGGameRecordModel *gameRecordModel = self.dataArray[indexPath.row];
//    NSString *detail = [SGUtil compareCurrentTime:gameRecordModel.created_at];
    [cell drollRecordCellWithIconStr:gameRecordModel.user.avatar nameStr:gameRecordModel.user.nickname  detailStr:gameRecordModel.created_at arrowResult:gameRecordModel.operate_result];
    return cell;
}


- (void)setupUI{
    [self addSubview:self.tableView];
    
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadNewData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 上拉刷新
        [weakSelf loadMoreData];
    }];
}

#pragma mark - loadData
- (void)loadDataWithPage:(NSInteger)page{
    
    NSInteger per_page = 20;
    
    [QYGNetRequestModel getGameRecordWithDeviceId:self.deviceId page:page per_page:per_page Completed:^(NSArray *gameRecordArr) {
        
        [self endRefresh];
        [self.dataArray addObjectsFromArray:gameRecordArr];
        [self.tableView reloadData];
        self.page = page;
    } WithErrorCodeBlock:^(NSError *error){
        
        [self endRefresh];
    }];
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadMoreData{
    
    [self loadDataWithPage:self.page + 1];
}

- (void)loadNewData {
    
    self.dataArray = [NSMutableArray array];
    [self loadDataWithPage:1];
}

- (void)setupData{
    [self.tableView.mj_header beginRefreshing];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.tableView.frame = self.bounds;
}
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        [_tableView registerClass:[QYGDetailCatchCell class] forCellReuseIdentifier:drollRecordID];
    }
    return _tableView;
}
@end
